package cn.com.shopec.erp.stock.commo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.com.shopec.erp.common.constant.ProductSCMConstant;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.product.entity.Defective;
import cn.com.shopec.erp.product.entity.ProductStock;
import cn.com.shopec.erp.product.vo.DefectReplceVo;
import cn.com.shopec.erp.purchase.entity.PurchaseOrder;
import cn.com.shopec.erp.purchase.entity.StockPurchase;
import cn.com.shopec.erp.purchase.vo.PurchaseOrderVo;
import cn.com.shopec.erp.purchase.vo.PurchaseRequisitionProductVo;
import cn.com.shopec.erp.purchase.vo.PurchaseReturnOrderVo;
import cn.com.shopec.erp.stock.entity.StockIn;
import cn.com.shopec.erp.stock.entity.StockInProduct;
import cn.com.shopec.erp.stock.vo.StockInProductVo;
import cn.com.shopec.erp.stock.vo.StockInVo;

public class StockInUtil {

	public static StockInVo parseStockVo(StockIn stockIn) {
		if (stockIn == null) {
			return null;
		}
		StockInVo stockInVo = new StockInVo();
		stockInVo.setId(stockIn.getId());
		stockInVo.setStockInNo(stockIn.getStockInNo());
		stockInVo.setStockInType(stockIn.getStockInType());
		stockInVo.setStockInTypeNo(stockIn.getStockInTypeNo());
		stockInVo.setStockInState(stockIn.getStockInState());
		stockInVo.setWhNo(stockIn.getWhNo());
		stockInVo.setPurNo(stockIn.getPurNo());
		stockInVo.setSupNo(stockIn.getSupNo());
		stockInVo.setCreater(stockIn.getCreater());
		stockInVo.setCreateTime(DateUtil.formatTimesTampDate(stockIn.getCreateTime()));
		stockInVo.setUpdater(stockIn.getUpdater());
		stockInVo.setUpdateTime(DateUtil.formatTimesTampDate(stockIn.getUpdateTime()));
		return stockInVo;
	}

	public static StockIn parseStockIn(StockInVo stockInVo) {
		if (stockInVo == null) {
			return null;
		}
		StockIn stockIn = new StockIn();
		stockIn.setId(stockInVo.getId());
		stockIn.setStockInNo(stockInVo.getStockInNo());
		stockIn.setStockInType(stockInVo.getStockInType());
		stockIn.setStockInTypeNo(stockInVo.getStockInTypeNo());
		stockIn.setStockInState(stockInVo.getStockInState());
		stockIn.setWhNo(stockInVo.getWhNo());
		stockIn.setPurNo(stockInVo.getPurNo());
		stockIn.setSupNo(stockInVo.getSupNo());
		return stockIn;
	}

	public static StockInProduct parseStockInProduct(StockInProductVo stockInProductVo) {
		if (stockInProductVo == null) {
			return null;
		}
		StockInProduct stockInProduct = new StockInProduct();
		stockInProduct.setId(stockInProductVo.getId());
		stockInProduct.setStockInNo(stockInProductVo.getStockInNo());
		stockInProduct.setSupNo(stockInProductVo.getSupNo());
		stockInProduct.setPurNo(stockInProductVo.getPurNo());
		stockInProduct.setCommoNo(stockInProductVo.getCommoNo());
		stockInProduct.setProdNo(stockInProductVo.getProdNo());
		stockInProduct.setProdCost(stockInProductVo.getProdCost());
		stockInProduct.setProdDesc(stockInProductVo.getProdDesc());
		stockInProduct.setProdTotal(stockInProductVo.getProdTotal());
		stockInProduct.setQualifiedQuantity(stockInProductVo.getQualifiedQuantity());
		stockInProduct.setDefectiveQuantity(stockInProductVo.getDefectiveQuantity());
		stockInProduct.setCreater(stockInProductVo.getCreater());
		stockInProduct.setCreateTime(stockInProductVo.getCreateTime());
		stockInProduct.setSupName(stockInProductVo.getSupName());
		stockInProduct.setPurProdNo(stockInProductVo.getPurProdNo());
		stockInProduct.setProductCodeState(stockInProductVo.getProductCodeState());
		return stockInProduct;
	}

	public static StockInProductVo parseStockInProductVo(StockInProduct stockInProduct) {
		if (stockInProduct == null) {
			return null;
		}
		StockInProductVo stockInProductVo = new StockInProductVo();
		stockInProductVo.setId(stockInProduct.getId());
		stockInProductVo.setStockInNo(stockInProduct.getStockInNo());
		stockInProductVo.setSupNo(stockInProduct.getSupNo());
		stockInProductVo.setPurNo(stockInProduct.getPurNo());
		stockInProductVo.setCommoNo(stockInProduct.getCommoNo());
		stockInProductVo.setProdNo(stockInProduct.getProdNo());
		stockInProductVo.setProdCost(stockInProduct.getProdCost());
		stockInProductVo.setProdDesc(stockInProduct.getProdDesc());
		stockInProductVo.setProdTotal(stockInProduct.getProdTotal());
		stockInProductVo.setQualifiedQuantity(stockInProduct.getQualifiedQuantity());
		stockInProductVo.setDefectiveQuantity(stockInProduct.getDefectiveQuantity());
		return stockInProductVo;
	}

	public static StockIn parseStock(PurchaseOrder purchase) {
		if (purchase == null) {
			return null;
		}
		StockIn stockIn = new StockIn();
		stockIn.setPurNo(purchase.getPoNo());
		stockIn.setPurNo(purchase.getPoName());
		stockIn.setStockInType(SZSCMConstant.STOCKIN_TYPE_1);
		return stockIn;
	}

	public static StockInVo parseStockVo(PurchaseOrderVo purchase) {
		if (purchase == null) {
			return null;
		}
		StockInVo stockIn = new StockInVo();
		stockIn.setPurNo(purchase.getPoNo());
		stockIn.setPurName(purchase.getPoName());
		stockIn.setStockInType(SZSCMConstant.STOCKIN_TYPE_1);
		stockIn.setSupName(purchase.getSupName());
		stockIn.setSupNo(purchase.getSupCode());
		return stockIn;
	}

	/**
	 * 采购单换货入库
	 * 
	 * @param purchase
	 * @return
	 */
	public static StockInVo purchaseReturnParseStockVo(PurchaseReturnOrderVo purchase) {
		if (purchase == null) {
			return null;
		}
		StockInVo stockIn = new StockInVo();
		stockIn.setPurNo(purchase.getPoNo());
		// stockIn.setPurName(purchase.getPoName());
		stockIn.setStockInTypeNo(purchase.getPuNo());
		stockIn.setStockInType(SZSCMConstant.STOCKIN_TYPE_4);
		stockIn.setSupName(purchase.getSupName());
		stockIn.setSupNo(purchase.getSupCode());
		stockIn.setWhNo(purchase.getPurWhNo());
		stockIn.setWhName(purchase.getPurWhName());
		stockIn.setPurNo(purchase.getPoNo());
		return stockIn;
	}

	public static StockInProductVo parseStockInProductVo(PurchaseRequisitionProductVo purchase) {
		if (purchase == null) {
			return null;
		}
		StockInProductVo stockInProduct = new StockInProductVo();
		stockInProduct.setProdNo(purchase.getProductNo());
		stockInProduct.setSupNo(purchase.getPpSupplierNo());
		stockInProduct.setSupName(purchase.getPpSupplierName());
		stockInProduct.setCommoNo(purchase.getCommodityNo());
		stockInProduct.setCommoName(purchase.getPpName());
		stockInProduct.setWhName(purchase.getPpWarehouseName());
		stockInProduct.setWhNo(purchase.getPpWarehouseNo());
		stockInProduct.setProdBarCode(purchase.getProdBarCode());
		stockInProduct.setSpecName(purchase.getPpSpecification());
		stockInProduct.setProdCost(Double.valueOf(purchase.getPpUnitPrice()));
		stockInProduct.setQualifiedQuantity(0l);
		stockInProduct.setDefectiveQuantity(0l);
		stockInProduct.setProdTotal(Long.valueOf(purchase.getPpNumber()));
		stockInProduct.setQualifiedQuantity(Long.valueOf(purchase.getPpNumber()));
		stockInProduct.setPurProdNo(purchase.getId());
		return stockInProduct;
	}

	public static StockInProduct parseStockInProduct(PurchaseRequisitionProductVo purchase) {
		if (purchase == null) {
			return null;
		}
		StockInProduct stockInProduct = new StockInProduct();
		stockInProduct.setProdNo(purchase.getProductNo());
		stockInProduct.setCommoNo(purchase.getCommodityNo());
		return stockInProduct;
	}

	public static List<StockInProductVo> parseStockInProductVo(List<PurchaseRequisitionProductVo> pagedBrandList) {
		List<StockInProductVo> stockInProduct = new ArrayList<StockInProductVo>();
		for (PurchaseRequisitionProductVo temp : pagedBrandList) {
			stockInProduct.add(parseStockInProductVo(temp));
		}
		return stockInProduct;
	}

	/**
	 * 采购换货转换器
	 * 
	 * @param pagedBrandList
	 * @return
	 */
	public static List<StockInProductVo> parseStockInPurchaseReturnVo(List<PurchaseReturnOrderVo> pagedBrandList) {
		List<StockInProductVo> stockInProduct = new ArrayList<StockInProductVo>();
		for (PurchaseReturnOrderVo temp : pagedBrandList) {
			stockInProduct.add(parseStockInPurchaseReturn(temp));
		}
		return stockInProduct;
	}

	public static StockInProductVo parseStockInPurchaseReturn(PurchaseReturnOrderVo purchase) {
		if (purchase == null) {
			return null;
		}
		StockInProductVo stockInProduct = new StockInProductVo();
		stockInProduct.setProdNo(purchase.getProdNo());
		stockInProduct.setSupNo(purchase.getSupCode());
		stockInProduct.setSupName(purchase.getSupName());
		stockInProduct.setCommoNo(purchase.getCommoNo());
		stockInProduct.setCommoName(purchase.getCommodityName());
		stockInProduct.setWhName(purchase.getPurWhName());
		stockInProduct.setWhNo(purchase.getPurWhNo());
		stockInProduct.setProdBarCode(purchase.getProdBarCode());
		stockInProduct.setSpecName(purchase.getSpecification());
		stockInProduct.setProdCost(Double.valueOf(purchase.getPurUnitPrice()));
		stockInProduct.setDefectiveQuantity(0l);
		stockInProduct.setProdTotal(Long.valueOf(purchase.getYieldAmountNumner()));
		stockInProduct.setQualifiedQuantity(Long.valueOf(purchase.getYieldAmountNumner()));
		stockInProduct.setPurProdNo(purchase.getId());
		return stockInProduct;
	}

	public static ProductStock parseProductStock(StockInVo stockIn, StockInProductVo stockInProduct) {
		ProductStock productStock = new ProductStock();
		productStock.setWhNo(stockIn.getWhNo());
		productStock.setSupNo(stockInProduct.getSupNo());
		productStock.setCommoNo(stockInProduct.getCommoNo());
		productStock.setProdNo(stockInProduct.getProdNo());
		productStock.setQualifiedQuantity(stockInProduct.getQualifiedQuantity());
		productStock.setDefectiveQuantity(stockInProduct.getDefectiveQuantity());
		productStock.setPreOccupyQuantity(0L);
		productStock.setVipOccupyQuantity(0L);
		productStock.setProductTotal(stockInProduct.getQualifiedQuantity() + stockInProduct.getDefectiveQuantity());
		return productStock;
	}

	public static Defective parseDefective(StockInVo stockInVo, StockInProductVo stockInProductVo) {
		Defective defective = new Defective();
		defective.setPoNo(stockInVo.getPurNo());
		defective.setWhNo(stockInVo.getWhNo());
		defective.setWhName(stockInVo.getWhName());
		defective.setDefectiveReason(stockInProductVo.getDefectiveReason());
		defective.setSupCode(stockInProductVo.getSupNo());
		defective.setSupName(stockInProductVo.getSupName());
		defective.setSpecName(stockInProductVo.getSupName());
		defective.setProdBarCode(stockInProductVo.getProdBarCode());
		defective.setProdNo(stockInProductVo.getProdNo());
		defective.setCommoNo(stockInProductVo.getCommoNo());
		defective.setCommoName(stockInProductVo.getCommoName());
		defective.setSpecName(stockInProductVo.getSpecName());
		defective.setDefectiveNumber(String.valueOf(stockInProductVo.getDefectiveQuantity()));
		Integer storageMethods = null;
		if (ObjectUtil.eq(stockInVo.getStockInType(), SZSCMConstant.STOCKIN_TYPE_1)) {
			storageMethods = ProductSCMConstant.STORAGEMETHODS_PURCHASE;
		} else if (ObjectUtil.eq(stockInVo.getStockInType(), SZSCMConstant.STOCKIN_TYPE_2)) {
			storageMethods = ProductSCMConstant.STORAGEMETHODS_STORAGE;
		} else {
			storageMethods = ProductSCMConstant.STORAGEMETHODS_OTHER;
		}
		defective.setAduitTime(DateUtil.formatTimesTampDate(new Date()));
		defective.setStorageMethods(storageMethods);
		defective.setStorageState(ProductSCMConstant.STORAGE_STATE_ADUITED);
		return defective;
	}

	public static StockInProductVo parseStockInProductVo(Map<String, Object> map) {
		if (map == null) {
			return null;
		}
		StockInProductVo stockInProductVo = new StockInProductVo();
		stockInProductVo.setProdBarCode(ObjectUtil.toString(map.get("prodBarCode")));
		stockInProductVo.setSpecification(ObjectUtil.toString(map.get("specName")));
		stockInProductVo.setProdNo(ObjectUtil.toString(map.get("prodNo")));
		stockInProductVo.setCommoName(ObjectUtil.toString(map.get("prodName")));
		stockInProductVo.setCommoNo(ObjectUtil.toString(map.get("commoNo")));
		stockInProductVo.setSupNo(ObjectUtil.toString(map.get("code")));
		stockInProductVo.setSupName(ObjectUtil.toString(map.get("supName")));
		stockInProductVo.setWhNo(ObjectUtil.toString(map.get("whNo")));
		stockInProductVo.setWhName(ObjectUtil.toString(map.get("whName")));
		stockInProductVo.setProdTotal(ObjectUtil.toLong(map.get("total")));
		stockInProductVo.setQualifiedQuantity(ObjectUtil.toLong(map.get("total")));
		stockInProductVo.setProdCost(ObjectUtil.toDoubleObject(map.get("price")));
		return stockInProductVo;
	}

	public static List<StockInProductVo> parseStockInProductVoForChannelOrder(List<Map<String, Object>> orderItems) {
		if (orderItems == null) {
			return null;
		}
		List<StockInProductVo> stockInProductVos = new ArrayList<StockInProductVo>();
		for (Map<String, Object> orderItem : orderItems) {
			StockInProductVo stockInProductVo = parseStockInProductVo(orderItem);
			stockInProductVos.add(stockInProductVo);
		}
		return stockInProductVos;
	}

	public static StockPurchase parseStockPurchase(StockInVo stockIn, ProductStock productStock,
			StockInProductVo stockInProductVo) {
		StockPurchase stockPurchase = new StockPurchase();
		stockPurchase.setPoNo(stockIn.getPurNo());
		stockPurchase.setPrqNo(stockInProductVo.getPurProdNo());
		stockPurchase.setStockNo(productStock.getId());
		stockPurchase.setRemainingAmount(stockInProductVo.getProdTotal());
		stockPurchase.setCommonNumner(stockInProductVo.getProdTotal());
		stockPurchase.setYieldNumner(stockInProductVo.getQualifiedQuantity());
		stockPurchase.setYieldAmountNumner(stockInProductVo.getQualifiedQuantity());
		stockPurchase.setDefectiveNumber(stockInProductVo.getDefectiveQuantity());
		stockPurchase.setDefectiveAmountNumber(stockInProductVo.getDefectiveQuantity());
		stockPurchase.setProdNo(stockInProductVo.getProdNo());
		stockPurchase.setCommoNo(stockInProductVo.getCommoNo());
		stockPurchase.setCommoName(stockInProductVo.getCommoName());
		stockPurchase.setSpecNo(stockInProductVo.getSpecification());
		stockPurchase.setSpecName(stockInProductVo.getSpecName());
		stockPurchase.setWhNo(stockIn.getWhNo());
		stockPurchase.setWhName(stockIn.getWhName());
		stockPurchase.setSupNo(stockInProductVo.getSupNo());
		stockPurchase.setSupName(stockInProductVo.getSupName());
		return stockPurchase;
	}

	public static StockInVo parseStockIn(DefectReplceVo defectReplceVo) {
		if (defectReplceVo == null) {
			return null;
		}
		StockInVo stockIn = new StockInVo();
		stockIn.setStockInTypeNo(defectReplceVo.getDefectReplceId());
		stockIn.setPurNo(defectReplceVo.getPoNo());
		stockIn.setSupName(defectReplceVo.getSupName());
		stockIn.setSupNo(defectReplceVo.getSupCode());
		stockIn.setWhNo(defectReplceVo.getWhNo());
		stockIn.setWhName(defectReplceVo.getWhName());
		return stockIn;
	}

	public static StockInProductVo parseStockInProductVo(DefectReplceVo defectReplceVo) {
		if (defectReplceVo == null) {
			return null;
		}
		StockInProductVo stockInProductVo = new StockInProductVo();
		stockInProductVo.setProdBarCode(defectReplceVo.getProdBarCode());
		stockInProductVo.setSpecName(defectReplceVo.getSpecName());
		stockInProductVo.setProdNo(defectReplceVo.getProdNo());
		stockInProductVo.setCommoName(defectReplceVo.getCommoName());
		stockInProductVo.setCommoNo(defectReplceVo.getCommoNo());
		stockInProductVo.setSupNo(defectReplceVo.getSupCode());
		stockInProductVo.setSupName(defectReplceVo.getSupName());
		stockInProductVo.setWhNo(defectReplceVo.getWhNo());
		stockInProductVo.setWhName(defectReplceVo.getWhName());
		stockInProductVo.setProdTotal(Long.valueOf(defectReplceVo.getDefectiveNumber()));
		stockInProductVo.setQualifiedQuantity(stockInProductVo.getQualifiedQuantity());
		stockInProductVo.setDefectiveQuantity(0l);
		stockInProductVo.setProdCost(0d);
		return stockInProductVo;
	}
}
